package com.ruoyi.quartz.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.WdyKline;
import com.ruoyi.system.service.IWdyKlineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component("klineTask")
public class KlineTask {

    @Autowired
    private IWdyKlineService wdyKlineService;

    public void calDailyKline() {
        Date calDate = DateUtils.addDays(new Date(), -1);
        String calDateStr = DateUtils.format(calDate, DateUtils.YYYY_MM_DD);
        QueryWrapper<WdyKline> klineQuery = new QueryWrapper<>();
        klineQuery.ge("stats_time", calDateStr + " 00:00:00");
        klineQuery.le("stats_time", calDateStr + " 23:59:59");
        klineQuery.eq("line_type", WdyKline.TYPE_DAY);
        if(wdyKlineService.count(klineQuery) > 0) {
            return;
        }
        WdyKline kline = wdyKlineService.calKline(calDate, WdyKline.TYPE_DAY);
        if(kline != null) {
            wdyKlineService.save(kline);
        }
    }
    public void calKline() {
        Date calDate = DateUtils.addMinutes(DateUtils.setSeconds(new Date(), 0), -1);
        String calDateStr = DateUtils.format(calDate, DateUtils.YYYY_MM_DD_HH_MM);
		QueryWrapper<WdyKline> klineQuery = new QueryWrapper<>();
		klineQuery.eq("stats_time", calDateStr + ":00");
        klineQuery.eq("line_type", WdyKline.TYPE_MINUTE);
		klineQuery.last("limit 1");
		WdyKline kline = wdyKlineService.getOne(klineQuery);
		if(kline != null) {
			return;
		}
        kline = wdyKlineService.calKline(calDate, WdyKline.TYPE_MINUTE);
		if(kline != null) {
		    wdyKlineService.save(kline);
        }
    }

    public void calKlineByTimeRange(String timeRange) {
        String[] timeArray = timeRange.split(",");
        Date startTime = DateUtils.parseDate(timeArray[0]);
        Date endTime = DateUtils.parseDate(timeArray[1]);
        List<WdyKline> list = new ArrayList<>();
        for(Date calDate = startTime; calDate.compareTo(endTime) <= 0; calDate = DateUtils.addMinute(calDate, 1)) {
            WdyKline kline = wdyKlineService.calKline(calDate, WdyKline.TYPE_MINUTE);
            if(kline != null) {
                list.add(kline);
            }
        }
        if(!CollectionUtils.isEmpty(list)) {
            wdyKlineService.saveBatch(list);
        }
    }

    public void calDailyKlineByTimeRange(String timeRange) {
        String[] timeArray = timeRange.split(",");
        Date startTime = DateUtils.parseDate(timeArray[0]);
        Date endTime = DateUtils.parseDate(timeArray[1]);
        List<WdyKline> list = new ArrayList<>();
        for(Date calDate = startTime; calDate.compareTo(endTime) <= 0; calDate = DateUtils.addDays(calDate, 1)) {
            WdyKline kline = wdyKlineService.calKline(calDate, WdyKline.TYPE_DAY);
            if(kline != null) {
                list.add(kline);
            }
        }
        if(!CollectionUtils.isEmpty(list)) {
            wdyKlineService.saveBatch(list);
        }
    }
}
